package com.xcalendar.database.dao;

import java.util.Date;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.google.appengine.api.datastore.Key;
import com.xcalendar.database.persistence.PMF;
import com.xcalendar.database.table.Event;
import com.xcalendar.database.table.User;

public class EventDAO {

	public void addEvent(Event event) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		try {
			txn.begin();
			User user = pm.getObjectById(User.class,event.getUserKey());
			user.getEvents().add(event);
			pm.makePersistent(user);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}

	public Event readEvent(Key key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Event event;
		try {
			event = pm.getObjectById(Event.class, key);
		} catch (JDOObjectNotFoundException e) {
			event = null;
		} finally {
			pm.close();
		}
		return event;
	}

	public void deleteEvent(Event event) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		try {
			txn.begin();
			User user = pm.getObjectById(User.class,event.getUserKey());
			event = pm.getObjectById(Event.class, event.getKey());
			user.getEvents().remove(event);
			pm.deletePersistent(event);
			pm.makePersistent(user);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}
	
	public void deleteEvent(Key eventKey) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		try {
			txn.begin();
			Event event = pm.getObjectById(Event.class, eventKey);
			User user = pm.getObjectById(User.class,eventKey.getParent());
			user.getEvents().remove(event);
			pm.deletePersistent(event);
			pm.makePersistent(user);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}

	public void updateEvent(Event event) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();

		String title = event.getTitle();
		Date startTime = event.getStartTime();
		Date endTime = event.getEndTime();
		Date remindTime = event.getRemindTime();
		boolean important = event.isImportant();
		String description = event.getDescription();
		Key locationKey = event.getLocationKey();
		System.out.println("dao:"+event.getLocationKey());
		try {
			txn.begin();
			event = pm.getObjectById(Event.class, event.getKey());
			event.setTitle(title);
			event.setStartTime(startTime);
			event.setEndTime(endTime);
			event.setRemindTime(remindTime);
			event.setImportant(important);
			event.setDescription(description);
			event.setLocationKey(locationKey);
			pm.makePersistent(event);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}

}